<?php
/**
 * Created by PhpStorm.
 * User: 76683
 * Date: 2018/7/17
 * Time: 22:05
 */

namespace app\api\controller;

use app\api\model\WeAds;
use app\api\model\WeTopicDetail as WeTopicDetailModel;
use app\api\model\WeNotice;
use app\api\model\BadWords;
use app\api\model\WeReport;
use think\Exception;
use think\Db;

class WeTopicDetail extends Base
{
    public function addDetail()
    {
        $uid = $this->uid;
        if (!$uid) {
            list($this->code, $this->msg) = [0, '请登陆'];
            return;
        }
        $data['topic_id'] = input('topic_id', '');
        $data['content'] = input('content', '');
        $request_time = $this->request->time();
        $data['user_id'] = $uid;
        $res = $this->validate($data, 'WeTopicDetail.add');
        if (true !== $res) {
            list($this->code, $this->msg) = [0, $res];
            return;
        }
        $model = new WeTopicDetailModel();
        //获取最新的回复
        $max_time = $model->where(['user_id' => $uid, 'topic_id' => $data['topic_id']])->max('create_time');
        $time = $request_time - $max_time;
        if ($time <= 60) {
            list($this->code, $this->msg) = [0, '您还有' . (60 - $time) . '秒可以在本树洞话题内回复'];
            return;
        }
        $data['content'] = BadWords::badWordsFilter(str_replace(' ', '', $data['content']));
        $result = $model->addTopicDetail($data);
        if ($result) {
            //消息通知
            $topic_user = \app\api\model\WeTopic::where(['id' => $data['topic_id']])->value('user_id');
            WeNotice::detail($topic_user, $data['topic_id']);
            list($this->code, $this->msg, $this->data) = [1, '评论成功', $result];
            return;
        } else {
            list($this->code, $this->msg) = [0, '评论失败'];
            return;
        }
    }

    /**
     * 评论点赞
     */
    public function vote()
    {
        $topic_id = input('detail_id', '0', 'intval');
        $uid = $this->uid;
        $data['detail_id'] = $topic_id;
        $data['user_id'] = $uid;
        $res = $this->validate($data, 'WeTopicDetail.vote');
        if (true !== $res) {
            list($this->code, $this->msg) = [0, $res];
            return;
        }
        $result = WeTopicDetailModel::vote($data);
        if (empty($result)) {
            list($this->code, $this->msg) = [0, '点赞失败'];
            return;
        } else {
            list($this->code, $this->msg) = [1, '点赞成功'];
            return;
        }
    }

    /**
     * 获取话题详情
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function topicDetail()
    {
        $data = $this->request->post();
        $res = $this->validate($data, 'WeTopicDetail.list');
        if (true !== $res) {
            list($this->code, $this->msg) = [0, $res];
            return;
        }
//        if (!$data['id']) {
//            list($this->code, $this->msg) = [0, '话题失效'];
//            return;
//        }
        $topic_info = \app\api\model\WeTopic::getTopicInfoByID($data['id'], $this->uid);
        if (empty($topic_info)) {
            list($this->code, $this->msg, $this->data) = [0, '话题失效', []];
        }
        $uid = $this->uid;
        $data = WeTopicDetailModel::detail($data, $uid);
        $weAds = new WeAds();
        $ads = $weAds->dayAds();
        $topic_info['detail'] = $data;
        $topic_info['ads'] = $ads;
        list($this->code, $this->msg, $this->data) = [1, '话题详情获取成功', $topic_info];
        return;
    }

    public function detailReport()
    {
        $data = $this->request->post();
        $uid = $this->uid;
        if (!$uid) {
            list($this->code, $this->msg) = [0, '请登录'];
        }
        $data['user_id'] = $uid;
        $res = $this->validate($data, ['value' => 'require|integer'], ['value.require' => '请选择正确回复', 'value.integer' => '请选择正确回复']);
        if (true !== $res) {
            list($this->code, $this->msg) = [0, $res];
            return;
        }
        //判断是否存在回复
        $detail_model = new WeTopicDetailModel();
        $detail_info = $detail_model->getDetail($data['value']);
        if (empty($detail_info)) {
            list($this->code, $this->msg) = [0, '错误回复ID'];
            return;
        }
        //判断是否举报过
        $report = new WeReport();
        $data['user_id'] = $uid;
        $data['type'] = 2;
        $exists = $report->alreadyReport($data);
        if ($exists) {
            list($this->code, $this->msg) = [0, '您已经举报过此回复'];
            return;
        }
        try {
            Db::startTrans();
            //更新话题字段
//            $update = $detail_model->reportDetail($data['value']);
//            if (!$update) {
//                throw new Exception('举报失败');
//            }
            //写入举报信息表
            $result = $report->reportDetail($data);
            if (!$result) {
                throw new Exception('举报失败');
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            list($this->code, $this->msg) = [0, $e->getMessage()];
            return;
        }
        list($this->code, $this->msg) = [1, '回复举报成功'];
        return;
    }
}